//
//  PropertyDetailViewController.m
//  YunGui
//
//  Created by HanenDev on 15/11/13.
//  Copyright © 2015年 hanen. All rights reserved.
//

#import "PropertyDetailViewController.h"

@interface PropertyDetailViewController ()
{
    UIScrollView *scrollView;
    NSArray      *_labelArray;
    
}
@property(nonatomic,strong)UILabel *propertyLab;
@property(nonatomic,strong)UILabel *locationLab;
@property(nonatomic,strong)UILabel *addressLab;
@property(nonatomic,strong)UILabel *phoneLab;
@property(nonatomic,strong)UILabel *numberLab;
@property(nonatomic,strong)UILabel *bankLab;
@property(nonatomic,strong)UILabel *bankIDLab;
@property(nonatomic,strong)UILabel *typeLab;
@property(nonatomic,strong)UILabel *detailBankLab;

@end

@implementation PropertyDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = GrayBackColor;
    self.navigationItem.hidesBackButton = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [ViewTool setNavigationBar:self.navigationController.navigationBar];
    
    self.navigationItem.title = @"物业详情";
    self.navigationItem.leftBarButtonItem = [ViewTool getBarButtonItemWithTarget:self WithString:@"back" WithAction:@selector(goToBack)];
    
    [self initViews];
    
    [self initData:_proId];
    
    
}
- (void)initData:(int)proid{
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@"app.property.detail" forKey:@"method"];
    NSNumber *numm = [NSNumber numberWithInt:proid];
    [dic setObject:numm forKey:@"pro_id"];
    
    [DataTool postWithUrl:HEAD_URL parameters:[DataTool addSignToDic:dic] success:^(id data) {
        
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@+--+",jsonDic);
        if ([jsonDic[@"status"] isEqualToString:@"ERROR"]) {
            [Tools showMessage:jsonDic[@"msg"] view:self];
            return ;
        }else{
        NSDictionary *dicc = jsonDic[@"data"];
        _propertyLab.text = dicc[@"title"];
        NSString *place;
        if ([dicc[@"province"] isEqualToString:dicc[@"city"]]) {
            place = [NSString stringWithFormat:@"%@ %@",dicc[@"province"],dicc[@"district"]];
        }else{
            place = [NSString stringWithFormat:@"%@ %@ %@",dicc[@"province"],dicc[@"city"],dicc[@"district"]];
        }
        _locationLab.text = place;
        _addressLab.text = dicc[@"address"];
        _phoneLab.text = dicc[@"tel"];
        
        NSArray *array = dicc[@"bankInfo"];
        if (array.count > 0) {
            NSDictionary *infoDic = array[0];
            _numberLab.text = [NSString stringWithFormat:@"NO.%@",infoDic[@"id"]];//编号
            _bankIDLab.text = infoDic[@"account"];

            int bankList  = [infoDic[@"bank_id"]intValue];
            if (bankList !=0 ) {
                for (int i=0; i<_bankListArr.count; i++) {
                    NSDictionary *bankDic = _bankListArr[i];
                    if (bankList == [bankDic[@"id"] intValue] ) {
                        _bankLab.text = bankDic[@"bank_name"];
                    }
                }
            }
            
            int bankType  = [infoDic[@"account_type"]intValue];
            if (bankType != 0) {
                for (int i=0; i<_bankTypeArr.count; i++) {
                    NSDictionary *bankDic = _bankTypeArr[i];
                    if (bankType == [bankDic[@"id"] intValue] ) {
                        _typeLab.text = bankDic[@"title"];
                    }
                }

            }
            
            _detailBankLab.text = infoDic[@"belong_to"];
        }
    }
    } fail:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
        [Tools showMessage:error.localizedDescription view:self];
    }];
    

}

- (void)initViews{
    scrollView =[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:scrollView];
    
    CGFloat startX = [UIView getWidth:20];
    CGFloat imgW = 20;
    CGFloat labX = startX + imgW +15;
    CGFloat labH = 40;
    CGFloat labY = 10;
    CGFloat height = labH - 2*labY;
    CGFloat grayH = 15;
    CGFloat labW  = 80;
    CGFloat tfX = labX + labW +[UIView getWidth:5];
    CGFloat tfW = kScreenWidth - tfX - [UIView getWidth:15];
    
    _labelArray=@[@"物业名称",@"所属地区",@"具体地址",@"联系电话",@"编号",@"开户银行",@"银行账号",@"账户类型",@"开户银行详细地址"];
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 380)];
    bgView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:bgView];
    
    UIView *sectionView1 = [ViewTool getLineViewWith:CGRectMake(0, 0, kScreenWidth, grayH) withBackgroudColor:GrayBackColor];
    [bgView addSubview:sectionView1];
    
    UIView *line = [self getLineWith:CGRectMake(0, sectionView1.maxY - 1, kScreenWidth, 1)];
    [bgView addSubview:line];
    
    UIImageView *basicImg = [self imageWith:CGRectMake(startX, line.maxY+ labY, imgW, imgW) withName:@"Btn_Normal_Xinxi"];
    [bgView addSubview:basicImg];
    
    UILabel *basicLabel = [self getLabelWith:CGRectMake(labX, line.maxY + labY, labW, height) WithTitle:@"基本信息"];
    [bgView addSubview:basicLabel];
    
    UIView *line1 = [self getLineWith:CGRectMake(labX, basicLabel.maxY + labY - 1, kScreenWidth-labX, 1)];
    [bgView addSubview:line1];
    
    for (int i=0; i<4; i++) {
        UILabel *label = [self getLabelWith:CGRectMake(labX, line1.maxY + labY+ i*(height+5), labW, height) WithTitle:_labelArray[i] WithFontSize:14.0f WithTitleColor:GRAYCOLOR WithTextAlignment:NSTextAlignmentLeft];
        [bgView addSubview:label];
    }
    _propertyLab = [self getLabelWith:CGRectMake(tfX, line1.maxY + labY, tfW, height) WithTitle:@""];
    [bgView addSubview:_propertyLab];
    
    _locationLab = [self getLabelWith:CGRectMake(tfX, _propertyLab.maxY + 5, tfW, height) WithTitle:@""];
    [bgView addSubview:_locationLab];
    
    _addressLab = [self getLabelWith:CGRectMake(tfX, _locationLab.maxY + 5, tfW, height) WithTitle:@""];
    [bgView addSubview:_addressLab];
    
    _phoneLab = [self getLabelWith:CGRectMake(tfX, _addressLab.maxY + 5, tfW, height) WithTitle:@""];
    [bgView addSubview:_phoneLab];
    
     
    UILabel *myLabel = [self getLabelWith:CGRectMake(labX, _phoneLab.maxY + labY , labW, height) WithTitle:@"我的账户"];
    [bgView addSubview:myLabel];
    
    UIView *line3 = [self getLineWith:CGRectMake(labX, myLabel.maxY + labY - 1, kScreenWidth-labX, 1)];
    [bgView addSubview:line3];
    
    for (int i=0; i<5; i++) {
        UILabel *label = [self getLabelWith:CGRectMake(labX, line3.maxY + labY+ i*(height+5), labW, height) WithTitle:_labelArray[i+4] WithFontSize:14.0f WithTitleColor:GRAYCOLOR WithTextAlignment:NSTextAlignmentLeft];
        [bgView addSubview:label];
        if (i == 4) {
            label.frame = CGRectMake(labX, line3.maxY + labY+ i*(height+5), labW +50, height);
        }
    }
    
    _numberLab = [self getLabelWith:CGRectMake(tfX, line3.maxY + labY, tfW, height) WithTitle:@""];
    [bgView addSubview:_numberLab];
    
    _bankLab = [self getLabelWith:CGRectMake(tfX, _numberLab.maxY + 5, tfW, height) WithTitle:@""];
    [bgView addSubview:_bankLab];
    
    _bankIDLab = [self getLabelWith:CGRectMake(tfX, _bankLab.maxY + 5, tfW, height) WithTitle:@""];
    [bgView addSubview:_bankIDLab];
    
    _typeLab = [self getLabelWith:CGRectMake(tfX, _bankIDLab.maxY + 5, tfW, height) WithTitle:@""];
    [bgView addSubview:_typeLab];
    
    _detailBankLab = [self getLabelWith:CGRectMake(tfX +50 + [UIView getWidth:5], _typeLab.maxY + 5, tfW - 50, height) WithTitle:@""];
    [bgView addSubview:_detailBankLab];
    
    UIView *line4 = [self getLineWith:CGRectMake(0, bgView.height - 1, kScreenWidth, 1)];
    [bgView addSubview:line4];
    
    scrollView.contentSize = CGSizeMake(kScreenWidth, _detailBankLab.maxY);
}
- (UIImageView *)imageWith:(CGRect)frame withName:(NSString *)str{
    UIImageView *img = [[UIImageView alloc]initWithFrame:frame];
    img.image = [UIImage imageNamed:str];
    return img;
}
- (UILabel *)getLabelWith:(CGRect)frame WithTitle:(NSString *)title
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = title;
    label.textColor = BLACKCOLOR;
    label.textAlignment = NSTextAlignmentLeft;
    //label.font = [UIView getFontWithSize:fontSize];
    label.font = [ViewTool getFont:16.0f];;
    return label;
}
- (UILabel *)getLabelWith:(CGRect)frame WithTitle:(NSString *)title WithFontSize:(CGFloat)fontSize WithTitleColor:(UIColor *)color WithTextAlignment:(NSTextAlignment )textAlignment
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.text = title;
    label.textColor = color;
    label.textAlignment = textAlignment;
    label.font = [ViewTool getFont:fontSize];;
    return label;
}

-(UIView *)getLineWith:(CGRect)frame
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = GRAYLINECOLOR;
    return view;
}

- (void)goToBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
